<?php
namespace PsiFramework\Classes\Factory;

import("Classes.Util.Manager.ApplicationManager");

use \PsiFramework\Classes\Util\Manager\ApplicationManager;

/**
 * Bibliotheque
 * Gestion de ma bibliothèque
 *
 * @author Rémi San
 * @version beta
 */


/**
 * The ObjectFactory is used to get the correct Object instance
 *
 * @package Factories
 */
class ObjectFactory
{

    /**
     * Static function to retrieve a new instance of
     * the object based on its name
     *
     * @param string $objectName    the object name
     * @return mixed    $object        the wanted Object
     */
    public static function getNewObject(
        /*string*/ $objectName,
        /*bool*/ $qualified=false
    )
    {
        $class = self::importObject($objectName, $qualified);
        $class = getClassName($class);
        if (!class_exists($class)) {
            return null;
        }
        $obj = new $class();
        $obj->setObjectName($objectName);
        return $obj ;
    }

    /**
     * Static function to import the class of an object
     *
     * @param string $objectName    the object name
     * @return mixed    $object        the wanted Object
     */
    public static function importObject(
        /*string*/ $objectName,
        /*bool*/ $qualified=false
    )
    {
        $class = null ;
        if (!$qualified) {
            $applicationManager = ApplicationManager::getInstance();
            $application = $applicationManager->getApplication();
            $objectParameters = $application->getObjectParameter($objectName);
            if ($objectParameters == null) {
                return null ;
            }
            $class = $objectParameters->getClass();
        } else {
            $class = $objectName ;
        }

        if (is_null($class)) {
            return null ;
        }

        import($class);
        return $class ;
    }
}
